﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using DataAccess;
using SubSonic;
using System.Configuration;
public partial class User_Shares_Documents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        { SqlDataSourceDocs.ConnectionString = Global.GetConnectionString();
            SqlDataSourceDocEdit.ConnectionString = Global.GetConnectionString();
            Lblstatus.Text = "";
            if (!IsPostBack)
            {
                Session["Type"] = "بخشنامه";
           
            
           
           
            GridView1.DataBind();
            Page.DataBind();
            DataListFileDownload.DataBind();
            var thisUser = HttpContext.Current.User;
            if (thisUser.Identity.IsAuthenticated)
            {
                if (thisUser.IsInRole("admin")   || thisUser.IsInRole("employer")  )
                {
                    PanelUpload.Visible = true;
                    PanelDownload.Visible = true;
                    PanelEdit.Visible = true;
                }
                else if (thisUser.IsInRole("advisor") || thisUser.IsInRole("Surveyor") || thisUser.IsInRole("viewuser") || thisUser.IsInRole("bonyadshahrestan"))
                {
                    PanelUpload.Visible = false;
                    PanelDownload.Visible = true;
                    PanelEdit.Visible = false;
                }
                else if (thisUser.IsInRole("edituser") || thisUser.IsInRole("fourthfactor"))
                {
                    PanelUpload.Visible = true;
                    PanelDownload.Visible = true;
                    PanelEdit.Visible = false;
                }
                else
                {
                    PanelUpload.Visible = false;
                    PanelDownload.Visible = false;
                    PanelEdit.Visible = false;
                }
            }
            else
            {
                PanelUpload.Visible = false;
                PanelDownload.Visible = false;
                PanelEdit.Visible = false;
            }
            }
        }
        catch
        {
 
        }
    }
    protected void Multifileupload1_click(object sender, FileCollectionEventArgs e)
    {
        try
        {
            DataAccess.TblDocument Doc = new DataAccess.TblDocument();
            Doc.Description = txtdescription.Text;
            Doc.Type = DropDownListType.SelectedValue;
            Doc.Save();
            object iddoc = Doc.GetPrimaryKeyValue();
            string folder = ConfigurationManager.AppSettings["saveFolder"];
            bool result = FileTools.SaveFiles(e, HttpContext.Current, folder,Guid.Empty, (Guid)iddoc,DateTime.Now);
            if (result == true)
            {
                Lblstatus.Text = string.Format("<script>alert(' فایل با موفقیت ارسال شد.')</script>");
                txtdescription.Text = "";
            }
            else
                Lblstatus.Text = string.Format("<script>alert(' ارسال فایل با خطا مواجه شد.')</script>");
            
        }
        catch { }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(DropDownListTypeDown.SelectedValue=="اطلاعیه")
        Session["Type"] = "اطلاعیه";
        if (DropDownListTypeDown.SelectedValue == "بخشنامه")
            Session["Type"] = "بخشنامه";
        if (DropDownListTypeDown.SelectedValue == "مکاتبات")
            Session["Type"] = "مکاتبات";
        if (DropDownListTypeDown.SelectedValue == "متفرقه")
            Session["Type"] = "متفرقه";
    }

   
    protected void ClickDownload(object sender, EventArgs e)
    {
        LinkButton linkButton = sender as LinkButton;
        DataListItem dataListItem = linkButton.NamingContainer as DataListItem;
        Guid id = new Guid(linkButton.CommandArgument.ToString());
        TblFileCollection tblFileCollection = new TblFileCollection();
        tblFileCollection.LoadAndCloseReader(TblFile.FetchByParameter(
            TblFile.Columns.Id, Comparison.Equals, id));
        switch (System.IO.Path.GetExtension(tblFileCollection[0].OriginalFileName))
        {
            case "jpg":
                Response.ContentType = "image.jpg";
                break;
            case "pdf":
                Response.ContentType = "application/pdf";
                break;
            case "doc":
            case "docx":
                Response.ContentType = "application/msword";
                break;
            case "dwg":
                Response.ContentType = "image/vnd.dwg";
                break;
            default:
                Response.ContentType = "application/octet-stream";
                break;
        }

        Response.AppendHeader("Content-Disposition", "attachment; filename=" + tblFileCollection[0].OriginalFileName);

        Response.TransmitFile(Server.MapPath("~/Files/" + tblFileCollection[0].FileName));

        Response.End();

    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onclick", "return confirm('آيا از حذف مطمئن هستيد؟');");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            string s = PersianDateControls.Convertor.ToPersianDate(Convert.ToDateTime(e.Row.Cells[7].Text)) + " " + Convert.ToDateTime(e.Row.Cells[7].Text).ToShortTimeString().Replace("AM", "صبح").Replace("PM", "عصر");
            e.Row.Cells[7].Text = s;
        }
    }
}